package com.xyht.sca_s.student_manage_system.modules.asset.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.req.SmsAssetFixedStorageBatchImportReq;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.req.SmsAssetFixedStorageExportReq;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.req.SmsAssetFixedStorageReq;
import com.xyht.sca_s.student_manage_system.modules.asset.service.SmsAssetFixedStorageService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * <p>
 * 固定资产库存 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-05-14
 */
@RestController
@RequestMapping("/asset/storage/fixed")
@SecretBody
public class SmsAssetFixedStorageController {
    @Autowired
    private SmsAssetFixedStorageService smsAssetFixedStorageService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "资产批量导入")
    @PostMapping("/batch")
    public ResponseResult batchAddFixedStorage(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsAssetFixedStorageService.batchAddFixedStorage(map, user_id);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiModelProperty(value = "资产单个添加")
    @PostMapping("/add")
    public ResponseResult singleAddFixedStorage(@RequestBody @Validated SmsAssetFixedStorageReq storageReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsAssetFixedStorageService.singleAddFixedStorage(storageReq, user_id);
    }
    @ApiModelProperty(value = "资产单个修改")
    @PostMapping("/update")
    public ResponseResult updateFixedStorage(@RequestBody SmsAssetFixedStorageReq storageReq) {
        return smsAssetFixedStorageService.updateFixedStorage(storageReq);
    }

    @ApiModelProperty(value = "获取库存列表")
    @GetMapping("/list")
    public ResponseResult getFixedStorage(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "search_str", required = false) String search_str,
                                                @RequestParam(value = "asset_name", required = false) String asset_name,
                                                @RequestParam(value = "asset_model", required = false) String asset_model,
                                                @RequestParam(value = "asset_brand", required = false) String asset_brand,
                                                @RequestParam(value = "asset_category_id", required = false) String asset_category_id,
                                                @RequestParam(value = "asset_status", required = false) Integer asset_status,
                                                @RequestParam(value = "asset_id", required = false) String asset_id,
                                                @RequestParam(value = "asset_price", required = false) BigDecimal asset_price,
                                                @RequestParam(value = "asset_place", required = false) String asset_place
                                          ) {
        return smsAssetFixedStorageService.getFixedStorage(search_str, asset_name, asset_model,
                asset_brand, asset_category_id, asset_status,asset_id,asset_price,asset_place,
                pageNum, pageSize);
    }

    @ApiModelProperty(value = "获取库存资产分类")
    @GetMapping("/group")
    public ResponseResult getExistAssetFixedStorageGroup(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "search_str", required = false) String search_str,
                                                @RequestParam(value = "asset_category_id", required = false) String asset_category_id
                                          ) {
        return smsAssetFixedStorageService.getExistAssetFixedStorageGroup(search_str,asset_category_id, pageNum, pageSize);
    }


    @ApiModelProperty(value = "资产批量更新导入")
    @PostMapping("/batchUpdate")
    public ResponseResult batchUpdateFixedStorage(@RequestBody SmsAssetFixedStorageBatchImportReq map, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsAssetFixedStorageService.batchUpdateFixedStorage(map, user_id);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

    @ApiModelProperty(value = "资产批量导出")
    @PostMapping("/export")
    public ResponseResult exportAssetFixedStorage(@RequestBody SmsAssetFixedStorageExportReq map, HttpServletResponse response) {
        ResponseResult commonCodeEnum;
        try {
            Future<ResponseResult> future = smsAssetFixedStorageService.exportAssetFixedStorage(map, response);
            while (true) {
                if (future.isDone()) {
                    commonCodeEnum = future.get();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }

}

